﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

using System.Net;
using System.Data;
using DTN.Core;
using Hanel.DTN.News;
using System.Text;
using System.IO;

public partial class News : System.Web.UI.Page
{
    string NameWeb = "Tin Tuc";// ten web thay doi de ghi len title
    ItblContentManager obj = tblContentManager.CreateInstance();
    ItblOS_Crawer_ChannelsManager objChannel = tblOS_Crawer_ChannelsManager.CreateInstance();
    ItblCommentManager objCom = tblCommentManager.CreateInstance();
    ItblEventManager objEvent = tblEventManager.CreateInstance();
    private static int countnew = 0;

    protected override void OnLoadComplete(System.EventArgs e)
    {
        base.OnLoadComplete(e);
        countnew++;
        if (countnew % 2 == 0)
        {
            if (Request.QueryString["ID"] != null)
            {
                CountNews(int.Parse(Request.QueryString["ID"]));
                countnew = 0;
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        LoadData();
       
        
    }
    protected void LoadData()
    {
        if (Request.QueryString["ID"] != null || Request.QueryString["parent"] != null || Request.QueryString["channel"] != null || Request.QueryString["key"] != null||Request.QueryString["event"]!=null)
        {
            if (Request.QueryString["ID"] != null)
            {
                this.PanelNewIn.Visible = true;
                this.PanelNew.Visible = false;
                DisplayNews();
            }
            if (Request.QueryString["parent"] != null)
            {
                this.PanelNewIn.Visible = false;
                this.PanelNew.Visible = true;
                DisplayParentNews();

            }
            if (Request.QueryString["channel"] != null)
            {
                this.PanelNewIn.Visible = false;
                this.PanelNew.Visible = true;
                DisplayChannelNews();
            }
            if (Request.QueryString["key"] != null)
            {
                this.PanelNewIn.Visible = false;
                this.PanelNew.Visible = true;
                DisplaySearch();
            }
            if (Request.QueryString["event"] != null)
            {
                this.PanelNewIn.Visible = false;
                this.PanelNew.Visible = true;
                DisplayEvent();
            }
        }
        else
        {
            Response.Redirect("Home.html");
        }
    }
    protected void CountNews(int id)
    {
        if (Request.QueryString["ID"] != null)
        {

            
            tblContent content = obj.SelecttblContentByID(id);

            int visited = int.Parse(content.Visited.ToString());
            content.Visited = visited + 1;

            obj.UpdatetblContent(content);
        }
        else
        {

        }

    }

    protected void DisplaySearch()
    {
        if (Request.QueryString["key"] != null)
        {
            string key = Request.QueryString["key"].ToString();
            DataTable dt = obj.SelecttblContentDynamic("status=1 and Content like N'%" + key + "%' or Title like N'%" + key + "%' or Header like N'%" + key + "%'", "ID desc");
            DataList2.DataSource = dt;
            //DataList2.DataBind();
            HplCha.Text = "Tìm kiếm";
            if (dt.Rows.Count < 9)
            {
                CollectionPager1.Visible = false;
                DataList2.DataBind();
            }
            else
            {
                CollectionPager1.Visible = true;
                CollectionPager1.PageSize = 8;
                CollectionPager1.DataSource = dt.DefaultView;
                CollectionPager1.BindToControl = DataList2;
                DataList2.DataSource = CollectionPager1.DataSourcePaged;
            }
            Page.Title ="Tìm kiếm: "+ key + " - " + NameWeb;

        }
        else
        {
            Response.Redirect("Home.html");
        }
    }

    protected void DisplayParentNews()
    {

        if (Request.QueryString["parent"] != null)
        {
            string sReturn = string.Empty;
            string sTemp = string.Empty;
            int parentID = int.Parse(Request.QueryString["parent"].ToString());
            DataTable dt = objChannel.SelectOS_Crawer_ChannelsDynamic("ParentID=" + parentID, "ID desc");

            
            for (int i = 0; i < int.Parse(dt.Rows.Count.ToString()); i++)
            {
                sTemp+="ChannelID="+ dt.Rows[i]["ID"].ToString()+" or ";
                
            }
            sReturn = sTemp.ToString();

            DataTable ds = obj.SelecttblContentDynamic("status=1 and "+sTemp+" ChannelID="+parentID, "ID desc");
            DataList2.DataSource = ds;
            if (ds.Rows.Count < 9)
            {
                CollectionPager1.Visible = false;
                DataList2.DataBind();
            }
            else
            {
                CollectionPager1.Visible = true;
                CollectionPager1.PageSize = 8;
                CollectionPager1.DataSource = ds.DefaultView;
                CollectionPager1.BindToControl = DataList2;
                DataList2.DataSource = CollectionPager1.DataSourcePaged;
            }

            tblOS_Crawer_Channels channel = objChannel.SelectOS_Crawer_ChannelsByID(parentID);
            HplCha.Text = channel.Name.ToString();
            HplCha.NavigateUrl = "News.aspx?parent=" + parentID.ToString();
            Page.Title = channel.Name.ToString() + " - " + NameWeb;
        }
        else
        {
            Response.Redirect("Home.html");
        }

    }

    protected void DisplayChannelNews()
    {
        if (Request.QueryString["channel"] != null)
        {
            int ChannelID = int.Parse(Request.QueryString["channel"].ToString());
            DataTable dt = obj.SelecttblContentDynamic("status=1 and ChannelID=" + ChannelID, "ID desc");
            DataList2.DataSource = dt;
            if (dt.Rows.Count < 9)
            {
                CollectionPager1.Visible = false;
                DataList2.DataBind();
            }
            else
            {
                CollectionPager1.Visible = true;
                CollectionPager1.PageSize = 8;
                CollectionPager1.DataSource = dt.DefaultView;
                CollectionPager1.BindToControl = DataList2;
                DataList2.DataSource = CollectionPager1.DataSourcePaged;
            }

            tblOS_Crawer_Channels channel = objChannel.SelectOS_Crawer_ChannelsByID(ChannelID);
            HplCon.Text = channel.Name.ToString();
            HplCon.NavigateUrl = "News.aspx?channel=" + ChannelID.ToString();
            tblOS_Crawer_Channels parent = objChannel.SelectOS_Crawer_ChannelsByID(channel.ParentID);
            if (parent.ID != 0)
            {
                HplCha.Text = parent.Name.ToString() + "<span >  »</span>";
                HplCha.NavigateUrl = "News.aspx?parent=" + parent.ID.ToString();
                Page.Title = channel.Name.ToString() + " - " + NameWeb;
            }
            else
            {
                HplCha.Text = "Trang chủ" + "<span >  »</span>";
                HplCha.NavigateUrl = "Home.html";
            }
        }
        else
        {
            Response.Redirect("Home.html");
        }
    }

    protected void DisplayNews()
    {
        if (Request.QueryString["ID"] !=null)
        {
            int id = int.Parse(Request.QueryString["ID"].ToString());
            
            DataTable dt = obj.SelecttblContentDynamic_top("Top 1", "Status=1 and ID="+id, "ID desc");
        
            DataList1.DataSource = dt;
            DataList1.DataBind();

            int ChannelID = int.Parse(dt.Rows[0]["ChannelID"].ToString());
            
            tblOS_Crawer_Channels channel = objChannel.SelectOS_Crawer_ChannelsByID(ChannelID);
            HplCon.Text = channel.Name.ToString();
            HplCon.NavigateUrl = "News.aspx?channel="+channel.ID.ToString();

            tblOS_Crawer_Channels parent = objChannel.SelectOS_Crawer_ChannelsByID(channel.ParentID);
            if (parent.ID != 0)
            {
                HplCha.Text = parent.Name.ToString() + "<span >  »</span>";
                HplCha.NavigateUrl = "News.aspx?parent=" + parent.ID.ToString();
            }
            else
            {
                HplCha.Text = "Trang chủ" + "<span >  »</span>";
                HplCha.NavigateUrl = "Home.html";
            }
            TopNews(id,ChannelID);//goi ham load top3 tin moi o cuoi
            Page.Title = dt.Rows[0]["Title"].ToString()+" - "+NameWeb;
            lblTenweb.Text = NameWeb;
            Panelphanhoi.Visible = false;
            txtSenderHeader.Text = dt.Rows[0]["Title"].ToString();
        }
        else
        {
            Response.Redirect("Home.html");
        }
       
       
    }
    protected void TopNews(int ID, int ChannelID)
    {
        DataTable dt = obj.SelecttblContentDynamic_top("Top 3", "status=1 and ID<>"+ID+"and ChannelID="+ChannelID, "ID desc");
        dtlTinMoi.DataSource = dt;
        dtlTinMoi.DataBind();
        DataTable dt2 = obj.SelecttblContentDynamic_top("Top 5", "status=1 and ID !=" + ID+"and ID<"+dt.Rows[2]["ID"].ToString()+"and ChannelID="+ChannelID, "ID desc");
        dtlTinDaDua.DataSource = dt2;
        dtlTinDaDua.DataBind();
       
    }

    protected void DisplayEvent()
    {
        if (Request.QueryString["event"] != null)
        {
            int Event = int.Parse(Request.QueryString["event"].ToString());
            DataTable dt = obj.SelecttblContentDynamic("status=1 and Event=" + Event, "ID desc");
            DataList2.DataSource = dt;
            if (dt.Rows.Count < 9)
            {
                CollectionPager1.Visible = false;
                DataList2.DataBind();
            }
            else
            {
                CollectionPager1.Visible = true;
                CollectionPager1.PageSize = 8;
                CollectionPager1.DataSource = dt.DefaultView;
                CollectionPager1.BindToControl = DataList2;
                DataList2.DataSource = CollectionPager1.DataSourcePaged;
            }

            DataTable sukien = objEvent.SelecttblEventDynamic("id="+Event, "[Order] desc");

            HplCha.Visible = false;           
            HplCon.Text = sukien.Rows[0]["Name"].ToString();
            HplCon.NavigateUrl = "News.aspx?event=" + Event;
            Page.Title = sukien.Rows[0]["Name"].ToString() + " - " + NameWeb;
           
            
        }
        else
        {
            Response.Redirect("Home.html");
        }
    }

    //protected override void Render(HtmlTextWriter writer)
    //{
    //    if (Request.QueryString["ID"] != null)
    //    {
    //        int id = int.Parse(Request.QueryString["ID"].ToString());
    //        ItblContentManager obj = tblContentManager.CreateInstance();
    //        DataTable dt = obj.SelecttblContentDynamic_top("Top 1", "Status=1 and ID=" + id, "ID desc");
           
    //        string str = ConvertText.ConvertToUnSign(dt.Rows[0]["Title"].ToString());


    //        StringBuilder sb = new StringBuilder();
    //        StringWriter sw = new StringWriter(sb);
    //        HtmlTextWriter hWriter = new HtmlTextWriter(sw);
    //        base.Render(hWriter);
    //        string PageResult = sb.ToString();
    //        using (StreamWriter outfile =
    //        new StreamWriter(Server.MapPath(str+".html")))
    //        {
    //            outfile.Write(PageResult);
    //        }
    //        writer.Write(PageResult);
    //    }
    //    else
    //    {
    //    }
    //}
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        if (txtCaptchar.Text.Equals(Session["captcha"].ToString(), StringComparison.OrdinalIgnoreCase))
        {
            lblMessage.Visible = false;
            tblComment comment = new tblComment();
            comment.Content_ID = int.Parse(Request.QueryString["ID"].ToString());
            comment.HoTen = txtSenderName.Text;
            comment.Email = txtSenderEmail.Text;
            comment.TieuDe = txtSenderHeader.Text;
            comment.NoiDung = txtSenderContent.Text;
            comment.com_create = DateTime.Now;
            comment.active = 0;

            objCom.InserttblComment(comment);
            Panelphanhoi.Visible = true;
            PanelComment.Visible = false;
            lblPhanHoi.Text = "(Cảm ơn bạn đã gửi bài bình luận & đánh giá. Bình luận của bạn sẽ xuất hiện sau khi được duyệt)";
        }
        else
        {
            lblMessage.Text = "Vui lòng nhập đúng chuỗi xác nhận";
        }

        
        
       
    }
    public string BindComment() // hien thi comment gui doc gui len (sau khi da active comment do trong quan tri)
    {
        string data = string.Empty;
        try
        {
            int content_id = int.Parse(Request.QueryString["ID"].ToString());

            DataTable dt = objCom.SelecttblCommentDynamic("active=1 and content_ID="+content_id, "com_create desc");
            
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];

                data += " <div class=\"panel5\">";
                data += " <div class=\"pnl-content\">";
                data += " <div class=\"item odd\">";
                data += "<p> <span class=\"label\"><b>";
                data += "Bình luận: "+dr["TieuDe"];
                data += "</b></span>";
              
                data += "<p> <span class=\"label\">";
                data += dr["NoiDung"];
                data += "<br>";
                data += "<span class=\"ThoiGian\">";
                data += "Được gửi bởi: " + dr["Hoten"];
                data += "<br>";
                data += "</span>";
                data +="<br class=\"clear\"><span class=\"ThoiGian\">";
                
                data +="Gửi lúc: " + Utils.GetTime(DateTime.Parse(dr["com_Create"].ToString())) ;
                data += "</span>";
                data += "</span>";
                data += "</div>";
                data += "</div></div>";
            }
        }
        catch
        {
            return data;
        }
        return data;
    }
}